#include "camera.h"
#include "globals.h"

Camera::Camera()
{
    cam.x = 0;
    cam.y = 0;
    cam.w = 0;
    cam.h = 0;

    move = false;
}

void Camera::set_cam(int x, int y)
{
    cam.x = x;
    cam.y = y;
}

void Camera::setup()
{
    cam.w = cfg.get_screen_width();
    cam.h = cfg.get_screen_height();
}

void Camera::update()
{
    if (move)
    {
        SDL_Rect mapBox = mm.get_box();
        SDL_Rect mapCam = mm.get_cam();
        position mouse = in.mousePosition();

        cam.x = float(mouse.x - mapBox.x - mapCam.w / 2.f) / mapBox.w * cfg.get_map_width();
        cam.y = float(mouse.y - mapBox.y - mapCam.h / 2.f) / mapBox.h * cfg.get_map_height();
    }
    else
    {
        if (in.isKeyDown(SDLK_UP))
            cam.y -= 10;

        if (in.isKeyDown(SDLK_DOWN))
            cam.y += 10;

        if (in.isKeyDown(SDLK_LEFT))
            cam.x -= 10;

        if (in.isKeyDown(SDLK_RIGHT))
            cam.x += 10;

        /*position mouse = in.mousePosition();

        if (mouse.y - 20 < 0)
            cam.y -= 10;

        if (mouse.y + 20 > cam.h)
            cam.y += 10;

        if (mouse.x + 20 > cam.w)
            cam.x += 10;

        if (mouse.x - 20 < 0)
            cam.x -= 10;*/
    }

    if(cam.x < 0)
        cam.x = 0;

    if(cam.y < 0)
        cam.y = 0;

    if(cam.x > cfg.get_map_width() - cam.w)
        cam.x = cfg.get_map_width() - cam.w;

    if(cam.y > cfg.get_map_height() - cam.h)
        cam.y = cfg.get_map_height() - cam.h;

    mm.update_camera(position(cam.x, cam.y));
}

void Camera::moveCamera(bool newMove)
{
    move = newMove;
}

SDL_Rect Camera::get_cam()
{
    return cam;
}

int Camera::x()
{
    return cam.x;
}

int Camera::y()
{
    return cam.y;
}
